package org.apache.dolphinscheduler.api.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.dolphinscheduler.api.service.UserGroupWorkerService;
import org.apache.dolphinscheduler.api.service.WorkerGroupService;
import org.apache.dolphinscheduler.dao.entity.UserGroupWorker;
import org.apache.dolphinscheduler.dao.mapper.UserGroupWorkerMapper;
import org.apache.dolphinscheduler.dao.mapper.WorkerGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class UserGroupWorkerServiceImpl extends ServiceImpl<UserGroupWorkerMapper, UserGroupWorker> implements UserGroupWorkerService {
	
	@Autowired
	private WorkerGroupMapper workerGroupMapper;

	public List<UserGroupWorker> queryByGroupId(Integer userGroupId) {
		QueryWrapper<UserGroupWorker> qw = new QueryWrapper<UserGroupWorker>();
		qw.eq("user_group_id", userGroupId);
		List<UserGroupWorker> list = list(qw);
		if(list != null && list.size() > 0) {
			for(UserGroupWorker ug : list) {
				ug.setWorkerGroup(workerGroupMapper.selectById(ug.getWorkerId()));
			}
		}
		return list;
	}
	
	public List<Integer> getWorkIdList(Integer userGroupId) {
		QueryWrapper<UserGroupWorker> qw = new QueryWrapper<UserGroupWorker>();
		qw.eq("user_group_id", userGroupId);
		List<UserGroupWorker> list = list(qw);
		if(list != null && list.size() > 0) {
			List<Integer> workIds = new ArrayList<Integer>();
			list.forEach(ug -> {workIds.add(ug.getWorkerId());});
			return workIds;
		}
		return null;
	}
	
	public List<UserGroupWorker> queryByWorkId(Integer workId) {
		QueryWrapper<UserGroupWorker> qw = new QueryWrapper<UserGroupWorker>();
		qw.eq("worker_id", workId);
		List<UserGroupWorker> list = list(qw);
		if(list != null && list.size() > 0) {
			for(UserGroupWorker ug : list) {
				ug.setWorkerGroup(workerGroupMapper.selectById(ug.getWorkerId()));
			}
		}
		return list;
	}
	
	public void deleteByGroupId(Integer userGroupId) {
		QueryWrapper<UserGroupWorker> qw = new QueryWrapper<UserGroupWorker>();
		qw.eq("user_group_id", userGroupId);
		remove(qw);
	}
	
	public void deleteByWorkId(Integer workId) {
		QueryWrapper<UserGroupWorker> qw = new QueryWrapper<UserGroupWorker>();
		qw.eq("worker_id", workId);
		remove(qw);
	}
	
	public void saveUserGroupWorkByWorkId(Integer workId, List<Integer> userGroupIds) {
		if(userGroupIds != null && userGroupIds.size() > 0) {
			deleteByWorkId(workId);
			List<UserGroupWorker> list = new ArrayList<UserGroupWorker>();
			for(Integer userGroupId : userGroupIds) {
				UserGroupWorker ugw = new UserGroupWorker();
				ugw.setUserGroupId(userGroupId);
				ugw.setWorkerId(workId);
				list.add(ugw);
			}
			saveBatch(list);
		}
	}
	
	public void saveUserGroupWork(Integer userGroupId, List<Integer> workIdList) {
		if(workIdList != null && workIdList.size() > 0) {
			deleteByGroupId(userGroupId);
			List<UserGroupWorker> list = new ArrayList<UserGroupWorker>();
			for(Integer workId : workIdList) {
				UserGroupWorker ugw = new UserGroupWorker();
				ugw.setUserGroupId(userGroupId);
				ugw.setWorkerId(workId);
				list.add(ugw);
			}
			saveBatch(list);
		}
	}
}
